﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BaseUnits
{
    public class PowerPlant : IPowerPlant 
    {
        int powerPlantId;
        String description;
        String externalId;
        List<IPowerUnit> powerUnits;
        //IPowerGrid grid;

        public PowerPlant(int powerPlantId, String externalId, String description, List<IPowerUnit> powerUnits)
        {
            this.powerPlantId = powerPlantId;
            this.externalId = externalId;
            this.description = description;
            this.powerUnits = powerUnits; 
            //this.grid = grid;
        }

        public string  ExternalId
        {
	        get { return this.externalId; }
        }

        public int  PowerPlantId
        {
	        get { return this.powerPlantId; }
        }

        public string  Description
        {
	        get { return this.description; }
        }

        public List<IPowerUnit>  PowerUnits
        {
	        get { return this.powerUnits; }
        }
        public override string ToString()
        {
            return this.Description;
        }
        public void AddPowerUnit(IPowerUnit powerUnit)
        {
            BaseUnits.Builders.PowerPlantBuilder builder = new Builders.PowerPlantBuilder(new DatabaseInterface.Connection());
            builder.AddPowerUnit(this, powerUnit);
        }
        public double TotalElectricityDispatch(DateTime dateFrom, DateTime dateTo)
        {
            //int totalHours = TotalHoursInYear(year);
            double totalDispatch = 0;
            while (dateFrom <= dateTo)
            {
                foreach (IPowerUnit pu in this.powerUnits)
                {
                    totalDispatch = totalDispatch + pu.ElectricityDispatch(dateFrom);
                }
                dateFrom = dateFrom.AddHours(1);
            }
            return totalDispatch;
        }
        public double TotalOutgoing(DateTime dateFrom, DateTime dateTo)
        {
            //int totalHours = TotalHoursInYear(year);
            double totalOutgoing = 0;
            while (dateFrom <= dateTo)
            {
                foreach (IPowerUnit pu in this.powerUnits)
                {
                    totalOutgoing = totalOutgoing + pu.GetOutgoing(dateFrom);
                }
                dateFrom = dateFrom.AddHours(1);
            }
            return totalOutgoing;
        }
        public double TotalIncoming(DateTime dateFrom, DateTime dateTo)
        {
            //int totalHours = TotalHoursInYear(year);
            double totalIncoming = 0;
            while (dateFrom <= dateTo)
            {
                foreach (IPowerUnit pu in this.powerUnits)
                {
                    totalIncoming = totalIncoming + pu.GetIncoming(dateFrom);
                }
                dateFrom = dateFrom.AddHours(1);
            }
            return totalIncoming;
        }
        public double GetElectricityDispatch(DateTime date, int hour =-1)
        {
            double totalDispatch = 0;
            foreach (IPowerUnit pu in this.powerUnits)
            {
                totalDispatch = totalDispatch + pu.ElectricityDispatch(date);
            }
            return totalDispatch;
        }
        public double GetIncoming(DateTime date, int hour = -1)
        {
            double totalIncoming = 0;
            foreach (IPowerUnit pu in this.powerUnits)
            {
                totalIncoming = totalIncoming + pu.GetIncoming(date);
            }
            return totalIncoming;
        }
        public double GetOutgoing(DateTime date, int hour = -1)
        {
            double totalOutgoing = 0;
            foreach (IPowerUnit pu in this.powerUnits)
            {
                totalOutgoing = totalOutgoing + pu.GetOutgoing(date);
            }
            return totalOutgoing;
        }
        /// <summary>
        /// Returns the total amount of hours in year
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        private int TotalHoursInYear(int year)
        {
            int totalHours = 8760;
            if (DateTime.IsLeapYear(year))
            {
                totalHours += 24;
            }
            return totalHours;
        }
        /// <summary>
        /// Returns the Power Grid to which the power plant belongs
        /// </summary>
        //public IPowerGrid Grid
        //{
        //    get { return this.grid; }
        //}
    }
}
